<template>
  <div class="auth-container" :style="{ backgroundImage: 'url(/public/covers/12.jpg)' }">
    <el-card class="auth-card">
      <h2 class="title">注册</h2>
      <el-form :model="registerForm" label-width="120px" @submit.prevent="handleRegister">
        <el-form-item label="用户名">
          <el-input v-model="registerForm.username" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="registerForm.password" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="registerForm.email" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="registerForm.fullName" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="registerForm.gender" placeholder="请选择性别" class="input-full-width">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="registerForm.phone" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker v-model="registerForm.birthDate" type="date" placeholder="选择日期" class="input-full-width"></el-date-picker>
        </el-form-item>
        <el-form-item label="本科学校">
          <el-input v-model="registerForm.undergraduateSchool" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="目标院校">
          <el-input v-model="registerForm.targetUniversity" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item label="个人简介">
          <el-input type="textarea" v-model="registerForm.personalBio" autocomplete="off" class="input-full-width"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="button-container">
            <el-button type="primary" @click="handleRegister">注册</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import axiosInstance from '../plugins/axios' // 导入 axios 实例
import { ElMessage } from 'element-plus'

const registerForm = ref({
  username: '',
  password: '',
  email: '',
  fullName: '',
  gender: '',
  phone: '',
  birthDate: null,
  undergraduateSchool: '',
  targetUniversity: '',
  personalBio: '',
  role: 'ROLE_USER'
})

const handleRegister = async () => {
  try {
    const response = await axiosInstance.post('/users/register', registerForm.value)
    if (response.data.status === 200) {
      ElMessage.success(response.data.message)
      console.log('Register form submitted:', response.data)
    } else {
      ElMessage.error(response.data.message)
      console.error('Register form submitted:', response.data)
    }
  } catch (error) {
    ElMessage.error('注册失败，请稍后再试')
    console.error('Error submitting register form:', error)
  }
}
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  flex-direction: column;
  justify-content: center;
  background-size: cover;
  background-position: center;
  position: relative;
}

.auth-card {
  width: 400px;
  padding: 20px;
  text-align: center;
}

.title {
  text-align: center;
}

.input-full-width {
  width: 100%;
}

.button-container {
  display: flex;
  justify-content: center;
  padding-left: 30px;
}
</style>